package edu.zli.redis.service.impl;

import edu.zli.redis.bean.User;
import edu.zli.redis.service.UserService;
import edu.zli.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author ZerlindaLi create at 2021/12/16 15:40
 * @version 1.0.0
 * @description UserServiceImpl
 * 直接使用RedisTemplate和使用RedisUtils
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public User getUser(Long id) {
        User user = User.buildUser(id);
        redisTemplate.opsForValue().set(RedisUtils.cachePrefix+id.toString(), user, 60, TimeUnit.MICROSECONDS);
        return (User)new RedisUtils(redisTemplate).get("MyRedisTemplateCache::"+id.toString());
    }

    @Override
    public List<User> listUser() {

        List<User> users = User.buildUserList();
        redisTemplate.opsForValue().set("MyRedisTemplateCache::users", users, 60, TimeUnit.MICROSECONDS);

        return (List<User>)new RedisUtils(redisTemplate).get("MyRedisTemplateCache::users");
    }
}
